package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.border.LineBorder;

/**
 *
 * @author Quân
 */
public class ImageListShower extends javax.swing.JPanel {

    private List<BufferedImage> images = null;
    private List<File> files = new ArrayList<>();
    private SingleImageShower shower;
    private JPanel jPanelControll;
    private int current;
    private LineBorder border = new LineBorder(Color.WHITE, 2);
    private LineBorder selectedBorder = new LineBorder(Color.ORANGE, 2);

    public List<File> getFiles() {
        return files;
    }

    public void setFiles(List<File> files) {
        this.files = files;
    }

    public SingleImageShower getShower() {
        return shower;
    }

    public void setShower(SingleImageShower shower) {
        this.shower = shower;
    }

    public List<BufferedImage> getImages() {
        return images;
    }

    public void setImages(List<BufferedImage> images) {
        this.images = images;
    }

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    /**
     * Creates new form ImageListShower
     */
    public ImageListShower() {
        initComponents();
        initGUI();
    }

    private void initGUI() {
        SpringLayout layout = new SpringLayout();
        setLayout(layout);

        jPanelControll = new ControlPanel();
        this.add(jPanelControll);
        layout.putConstraint(SpringLayout.NORTH, jPanelControll, 5, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.EAST, jPanelControll, -5, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, jPanelControll, 5, SpringLayout.WEST, this);

        jScrollPane.setPreferredSize(new Dimension(100, 115));
        jScrollPane.getHorizontalScrollBar().setUnitIncrement(26);
        layout.putConstraint(SpringLayout.EAST, jScrollPane, -5, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.SOUTH, jScrollPane, -5, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.WEST, jScrollPane, 5, SpringLayout.WEST, this);

        shower = new SingleImageShower();
        shower.setOpaque(false);
        this.add(shower);
        layout.putConstraint(SpringLayout.NORTH, shower, 5, SpringLayout.SOUTH, jPanelControll);
        layout.putConstraint(SpringLayout.EAST, shower, -5, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.SOUTH, shower, -5, SpringLayout.NORTH, jScrollPane);
        layout.putConstraint(SpringLayout.WEST, shower, 5, SpringLayout.WEST, this);
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        jPanelThumnail.removeAll();
        if (images == null) {
            return;
        }
        for (BufferedImage image : images) {
            ImageIcon icon = new ImageIcon(image.getScaledInstance(80, 80, Image.SCALE_SMOOTH));
            Thumbnail thumbnail = new Thumbnail(icon, border);
            jPanelThumnail.add(thumbnail);
        }
        setCurrentImage(current);
    }

    public void setCurrentImage(int index) {
        if (jPanelThumnail.getComponentCount() > 0) {
            JLabel jLabelOld = (JLabel) jPanelThumnail.getComponent(current);
            jLabelOld.setBorder(border);
            JLabel jLabelNew = (JLabel) jPanelThumnail.getComponent(index);
            jLabelNew.setBorder(selectedBorder);
        }
        current = index;
        shower.setImage(images.get(index));
    }

    private void Next(int i) {
        int index = (current + i + images.size()) % images.size();
        setCurrentImage(index);
    }

    private void Zoom(int wheelRotation) {
        shower.Zoom(wheelRotation);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane = new javax.swing.JScrollPane();
        jPanelThumnail = new javax.swing.JPanel();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(51, 204, 0)));
        addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                formMouseWheelMoved(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        jPanelThumnail.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
        jScrollPane.setViewportView(jPanelThumnail);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(109, Short.MAX_VALUE)
                .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_formMouseWheelMoved
        if (evt.getModifiers() == MouseWheelEvent.CTRL_MASK) {
            Zoom(evt.getWheelRotation());
        } else {
            Next(evt.getWheelRotation());
        }
    }//GEN-LAST:event_formMouseWheelMoved

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
            this.setVisible(false);
        }
    }//GEN-LAST:event_formKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanelThumnail;
    private javax.swing.JScrollPane jScrollPane;
    // End of variables declaration//GEN-END:variables
}
